#include "Triangle.h"

Triangle::Triangle()
{
}

Triangle::Triangle(const Vector3<float> &aPoint1,const Vector3<float> &aPoint2,const Vector3<float> &aPoint3)
{
	Init(aPoint1, aPoint2, aPoint3);
}

Triangle::Triangle(const Utils::StaticArray<Vector3<float>, 3> &somePoints)
{
	Init(somePoints);
}

Triangle::~Triangle()
{
}

Triangle &Triangle::Init(const Vector3<float> &aPoint1,const Vector3<float> &aPoint2,const Vector3<float> &aPoint3)
{
	myPoints[0] = aPoint1;
	myPoints[1] = aPoint2;
	myPoints[2] = aPoint3;
	Vector3f center = (aPoint1 + aPoint2 + aPoint3) / 3;
	Vector3f normal = (aPoint2-aPoint1).Cross(aPoint3-aPoint1);
	Vector3f dummy;
	Intersection::LineSegment3D line;
	line.myStartPos = center + normal;
	line.myEndPos = center - normal;
	if (Intersection::LineVsTriangle(line, *this, dummy)==false)
	{
		dummy = myPoints[0];
		myPoints[0] = myPoints[1];
		myPoints[1] = dummy;
		if (Intersection::LineVsTriangle(line, *this, dummy)==false)
		{
			dummy = myPoints[1];
			myPoints[1] = myPoints[2];
			myPoints[2] = dummy;
		}
	}
	return *this;
}

Triangle &Triangle::Init(const Utils::StaticArray<Vector3<float>, 3> &somePoints)
{
	for (unsigned int index=0; index<somePoints.size(); ++index)
	{
		myPoints[index] = somePoints[index];
	}
	return *this;
}

Triangle &Triangle::operator=(const Triangle &aTriangle)
{
	for (unsigned int index=0; index<3; ++index)
	{
		myPoints[index] = aTriangle.myPoints[index];
	}
	return *this;
}

Triangle & Triangle::operator=(const Intersection::Triangle &aTriangle)
{
	for (unsigned int index=0; index<3; ++index)
	{
		myPoints[index] = aTriangle.myPoints[index];
	}
	return *this;
}